package com.fmsz.xmly.adapters.findrecom;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.findrecom.FindRecomVP1Entity;
import com.fmsz.xmly.entitys.findrecom.FindRecomVP2Entity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/3.
 */

public class FindRecomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //    private FindRecomVP1Adapter vp1Adapter;
        private static int[] vp2PointArr = {R.id.find_recom_point_1,R.id.find_recom_point_2};
    private static int[] vp2ImageIdArr = {R.id.find_recommend_vp2_iv1, R.id.find_recommend_vp2_iv2, R.id.find_recommend_vp2_iv3, R.id.find_recommend_vp2_iv4};
    private static int[] vp2TextIdArr = {R.id.find_recommend_vp2_tv1, R.id.find_recommend_vp2_tv2, R.id.find_recommend_vp2_tv3, R.id.find_recommend_vp2_tv4};
    private static List<ImageView> vp2PointList = new ArrayList<>();
    private List<FindRecomVP1Entity> vp1List;
    private List<FindRecomVP2Entity> vp2List;
    static final List<ImageView> vp1ImageList = new ArrayList<>();
    private Context context;
    private static LayoutInflater inflater;
    private static int vp1CurState;
    private static int vp1CurPosition;
    private RecyclerView.ViewHolder holder1 = null;
    private static boolean vp1IsIdle = true;
    private int count, threadCount;
    //    private RecyclerView.ViewHolder holder;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1 && vp1ImageList.size() > 0) {
                int currentItem = vp1CurPosition;
                currentItem++;
                ((ViewHolderVP1) holder1).find_recommend_vp_1.setCurrentItem(currentItem);
            } else if (msg.what == 2 && vp1CurState == 0) {
                vp1IsIdle = true;
            }
        }
    };

    public FindRecomAdapter(Context context, List<FindRecomVP1Entity> vp1List, List<FindRecomVP2Entity> vp2List) {
        this.vp1List = vp1List;
        this.vp2List = vp2List;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getItemViewType(int position) {
        int i = 0;
        switch (position) {
            case 0:
                i = 0;
                break;
            case 1:
                i = 1;
                break;
        }
        return i;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.find_recommend_vp1, parent, false);
                holder = new ViewHolderVP1(view);
                holder1 = holder;
                startVP1Thread();
                break;
        }
        return holder;
    }



    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                getVP1ImageList();
                ((ViewHolderVP1) holder).find_recommend_vp_1.setAdapter(new FindRecomVP1Adapter(vp1ImageList));
                ((ViewHolderVP1) holder).find_recommend_vp_1.setCurrentItem(1);
                ((ViewHolderVP1) holder).find_recommend_vp_1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageSelected(int position) {
                        vp1CurPosition = position;
                        if (position == 0) {
                            ((ViewHolderVP1) holder).find_recommend_vp_1.setCurrentItem(vp1ImageList.size() - 2, false);
                        } else if (position == vp1ImageList.size() - 1) {
                            ((ViewHolderVP1) holder).find_recommend_vp_1.setCurrentItem(1, false);
                        }
                        //TODO 根据vp1PointList,实现小点移动
                        /*for (int i = 0; i < vp1PointList.size(); i++) {
                            vp1PointList.get(i).setSelected(i == position);
                        }*/
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                        switch (state) {
                            case ViewPager.SCROLL_STATE_IDLE:
                                vp1CurState = 0;
                                handler.sendEmptyMessageDelayed(2, 0);
                                break;
                            case ViewPager.SCROLL_STATE_DRAGGING:
                                vp1IsIdle = false;
                                vp1CurState = 1;
                                break;
                        }
                    }
                });
                ((ViewHolderVP1) holder).find_recommend_vp_2.setAdapter(new FindRecomVP2Adapter(getVP2ViewList()));
                ((ViewHolderVP1) holder).find_recommend_vp_2.setCurrentItem(0);
                ((ViewHolderVP1) holder).find_recommend_vp_2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
                    @Override
                    public void onPageSelected(int position) {
                        for (int i = 0; i < vp2PointList.size(); i++) {
                            vp2PointList.get(i).setSelected(i==position);
                        }
                    }
                    @Override
                    public void onPageScrollStateChanged(int state) {}
                });
                break;

        }
    }


    @Override
    public int getItemCount() {
        return 1;
    }
    //发现-推荐-第一个子布局(ViewPager1)的ViewHolder
    public static class ViewHolderVP1 extends RecyclerView.ViewHolder {
        ViewPager find_recommend_vp_1;
        ViewPager find_recommend_vp_2;
        public ViewHolderVP1(View itemView) {
            super(itemView);
            find_recommend_vp_1 = (ViewPager) itemView.findViewById(R.id.find_recommend_vp_1);
            find_recommend_vp_2 = (ViewPager) itemView.findViewById(R.id.find_recommend_vp_2);
            initVP2Point(itemView);
            //TODO 添加小点给VP1
        }
    }
    //发现-推荐-第二个子布局(猜你喜欢)的ViewHolder
    public static class ViewHolderVP2 extends RecyclerView.ViewHolder {


        public ViewHolderVP2(View itemView) {
            super(itemView);

        }
    }


    //开启VP1的子线程
    private void startVP1Thread() {
        //VP1图片轮播子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    while (vp1IsIdle) {
                        try {
                            Thread.sleep(3000);
                            if (vp1IsIdle) {
                                handler.sendEmptyMessageDelayed(1, 0);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
    //实例化vp1ImageList
    private void getVP1ImageList() {
        if (vp1ImageList.size() > 0) {
            vp1ImageList.clear();
        }
        int[] imageArr = new int[vp1List.size() + 2];
        if (vp1List.size() > 0) {
            for (int i = 0; i < imageArr.length; i++) {
                ImageView image = new ImageView(context);
                image.setClickable(true);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                if (i == 0) {
                    Picasso.with(context).load(vp1List.get(vp1List.size() - 1).getPic()).into(image);
                } else if (i > 0 && i < imageArr.length - 1) {
                    Picasso.with(context).load(vp1List.get(i - 1).getPic()).into(image);
                } else if (i == imageArr.length - 1) {
                    Picasso.with(context).load(vp1List.get(0).getPic()).into(image);
                }
                vp1ImageList.add(image);
            }
        }
    }
    //    实例化VP2中的View
    private List<View> getVP2ViewList() {
        List<View> vp2ViewList = new ArrayList<>();
        View view1 = inflater.inflate(R.layout.find_recommend_vp2_item, null);
        View view2 = inflater.inflate(R.layout.find_recommend_vp2_item, null);
        for (int i = 0; i < vp2List.size(); i++) {
            if (i < vp2List.size() / 2) {
                ImageView vp2Image = (ImageView) view1.findViewById(vp2ImageIdArr[i]);
                TextView vp2Text = (TextView) view1.findViewById(vp2TextIdArr[i]);
                if (vp2List.get(i).getCoverPath() != null) {
                    Picasso.with(context).load(vp2List.get(i).getCoverPath()).into(vp2Image);
                }
                vp2Image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO 图片点击监听
                    }
                });
                vp2Text.setText(vp2List.get(i).getTitle());
            } else if (i >= 4) {
                ImageView vp2Image = (ImageView) view2.findViewById(vp2ImageIdArr[i - 4]);
                TextView vp2Text = (TextView) view2.findViewById(vp2TextIdArr[i - 4]);
                if (vp2List.get(i).getCoverPath() != null) {
                    Picasso.with(context).load(vp2List.get(i).getCoverPath()).into(vp2Image);
                }
                vp2Image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO 图片点击监听
                    }
                });
                vp2Text.setText(vp2List.get(i).getTitle());
            }
        }
        vp2ViewList.add(view1);
        vp2ViewList.add(view2);
        return vp2ViewList;
    }
    //实例化VP2的小点
    private static void initVP2Point(View itemView) {
        if (vp2PointList.size()>0) {
            vp2PointList.clear();
        }
        for (int i = 0; i < vp2PointArr.length; i++) {
            ImageView vp2Point = (ImageView) itemView.findViewById(vp2PointArr[i]);
            vp2PointList.add(vp2Point);
        }
        vp2PointList.get(0).setSelected(true);
    }
}
